<template>
  <div class="passenger">
    <a-card title="每日车厢">
      <a-form layout="inline" :model="formObj">
        <a-form-item>
          <a-date-picker
            v-model:value="formObj.date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
          />
        </a-form-item>
        <a-form-item>
          <TrainSelect v-model:code="formObj.trainCode" width="200px" />
        </a-form-item>
        <a-form-item>
          <a-select
            v-model:value="formObj.seatType"
            style="width: 200px"
            allow-clear
            placeholder="请选择车次类型"
          >
            <a-select-option
              v-for="(item, index) in getEnumList('seat_type').value"
              :key="index"
              :value="item.code"
            >
              <span>{{ item.description }}</span>
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="getList">查询</a-button>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="add">新增</a-button>
        </a-form-item>
        <a-form-item>
          <a-button @click="reset">重置</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-table :data-source="dataList" :columns="columns" :pagination="false">
      <template #action="{ record }">
        <a-button type="link" size="small" @click="edit(record)">编辑</a-button>
        &nbsp;&nbsp;&nbsp;
        <a-popconfirm title="确认删除吗?" @confirm="deleteFn(record)">
          <a-button type="link" danger size="small">删除</a-button>
        </a-popconfirm>
      </template>
    </a-table>
    <a-pagination
      v-model:current="formObj.page"
      v-model:page-size="formObj.pageSize"
      align="right"
      :total="formObj.total"
      show-size-changer
      style="margin-top: 20px"
      @change="onChange"
    />
    <a-modal
      v-model:open="dialog"
      :title="formState.id ? '编辑' : '新增'"
      width="700px"
      ok-text="确认"
      cancel-text="取消"
      center
      @ok="handleOk"
      @cancel="cancel"
    >
      <a-form :model="formState">
        <a-form-item label="日期">
          <a-date-picker
            v-model:value="formState.date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
          />
        </a-form-item>
        <a-form-item label="车次编号">
          <TrainSelect v-model:code="formState.trainCode" />
        </a-form-item>
        <a-form-item label="厢号">
          <a-input v-model:value="formState.index" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="座位类型">
          <a-select
            v-model:value="formState.seatType"
            style="width: 200px"
            allow-clear
            placeholder="请选择车次类型"
          >
            <a-select-option
              v-for="(item, index) in getEnumList('seat_type').value"
              :key="index"
              :value="item.code"
            >
              <span>{{ item.description }}</span>
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="排数">
          <a-input v-model:value="formState.rowCount" placeholder="请输入" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
<script setup lang="ts">
import { nextTick, onMounted, ref } from 'vue';
import { DailyCarriageApi } from '../../api/dailyCarriage';
import { message } from 'ant-design-vue';
import TrainSelect from '@/components/Train-Select.vue';
import { getEnumList } from '@/utils/getEnum';
const formState = ref<any>({
  id: '',
  trainCode: '',
  index: '',
  date: null,
  seatType: null,
  seatCount: '',
  rowCount: '',
  colCount: '',
});
const dialog = ref(false);

const columns = ref([
  {
    title: '日期',
    dataIndex: 'date',
    key: 'date',
  },
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
  },
  {
    title: '厢号',
    dataIndex: 'index',
    key: 'index',
  },
  {
    title: '座位类型',
    dataIndex: 'seatTypeName',
    key: 'seatTypeName',
  },
  {
    title: '座位数',
    dataIndex: 'seatCount',
    key: 'seatCount',
  },
  {
    title: '排数',
    dataIndex: 'rowCount',
    key: 'rowCount',
  },
  {
    title: '列数',
    dataIndex: 'colCount',
    key: 'colCount',
  },
  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    width: 200,
    slots: { customRender: 'action' }, // 使用 slots 而不是 render 函数
  },
]);
const formObj = ref({
  date: null,
  trainCode: '',
  seatType: null,
  page: 1,
  pageSize: 10,
  total: 0,
});
onMounted(() => {
  getList();
});
const dataList = ref([]);

const getList = async (): Promise<void> => {
  const { data } = await DailyCarriageApi.query(formObj.value);
  dataList.value = data.records || [];
  formObj.value.total = data.total;
};
const reset = (): void => {
  formObj.value.trainCode = '';
  formObj.value.date = null;
  formObj.value.seatType = null;
  getList();
};
const onChange = (current: number, pageSize: number): void => {
  console.log(current, pageSize);
  formObj.value.page = current;
  formObj.value.pageSize = pageSize;
  getList();
};
const add = (): void => {
  console.log('add');
  dialog.value = true;
};
const edit = async (record: any): Promise<void> => {
  console.log('record', record);
  const { data } = await DailyCarriageApi.detail(record.id);
  formState.value = data;
  nextTick(() => {
    dialog.value = true;
  });
};
const deleteFn = async (record: any): Promise<void> => {
  await DailyCarriageApi.delete(record.id);
  message.success('删除成功');
  getList();
};
const handleOk = async (): Promise<void> => {
  if (formState.value.id) {
    await DailyCarriageApi.update(formState.value);
  } else {
    await DailyCarriageApi.add(formState.value);
  }
  message.success('保存成功');
  dialog.value = false;
  getList();
};
const cancel = (): void => {
  Object.keys(formState.value).forEach(key => {
    formState.value[key] = '';
  });
  console.log('cancel');
};
</script>

<style scoped lang="less">
.passenger {
  :global(.ant-modal .ant-modal-footer) {
    text-align: center;
  }
}
</style>
